

#include "simple_protocol.h"
#include <Modules/BLE/BLE.h>
#include <Reader/Reader.h>

#undef LOCAL_TRACE
#define LOCAL_TRACE 1

#undef LOCAL_LOG
#define LOCAL_LOG 1

#include <Log.h>

/*--------------------------------------------------------------------------------------------------*/
/*
1.5.31.3. BLEInit
Command: [2502][Byte: Mode]
Response: [00][Bool: Result]
Example
Command: 250201
(Mode: 01)
Response: 0001
(Result: true)
*/
static int api_BLE_Init(buffer_t* command, buffer_t* response){
	typedef struct __PACKED  {
		uint8_t Mode;
	}BLE_Init_param_t;
	BLE_Init_param_t* param = (BLE_Init_param_t*)command->data;
	LTRACE_D("Mode=%d", param->Mode);

	if(param->Mode == 0x00){
		param->Mode = 0x01;
	}
	else
	if(param->Mode == 0xFF){
		param->Mode = 0x00;
	}
	
	int ret = BLE_Init(param->Mode);
	API_CHECK(ret);  	  //处理通讯错误
	
	reader.ble.flag = param->Mode;
	return ERR_NONE;
}

#if 0
static int api_BLE_Init(buffer_t* command, buffer_t* response){
	typedef struct __PACKED  {
		uint8_t Mode;
	}BLE_Init_param_t;
	BLE_Init_param_t* param = (BLE_Init_param_t*)command->data;
	LTRACE_D("Mode=%d", param->Mode);
	NET_BUF_SIMPLE_DEFINE(cmd_buf, 1);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 16);
	BLEPack_T cmd = {.code = BLE_Cmd_Init, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};

	if(param->Mode == 0x00){
		param->Mode = 0x01;
	}
	else
	if(param->Mode == 0xFF){
		param->Mode = 0x00;
	}
	
	net_buf_simple_add_u8(cmd.data, param->Mode);
	int ret = BLE_Command2(&cmd, &resp, 200);
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
	return ERR_NONE;
}
#endif 

int open_BLE_Init(void){

	NET_BUF_SIMPLE_DEFINE(cmd_buf, 1);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 16);
	BLEPack_T cmd = {.code = BLE_Cmd_Init, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};
	
	net_buf_simple_add_u8(cmd.data, 0x01);	// 开启蓝牙
	int ret = BLE_Command2(&cmd, &resp, 100);
	
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
		
	return ERR_NONE;
}

int close_BLE_Init(void){

	NET_BUF_SIMPLE_DEFINE(cmd_buf, 1);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 16);
	BLEPack_T cmd = {.code = BLE_Cmd_Init, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};

	net_buf_simple_add_u8(cmd.data, 0x00);	// 关闭蓝牙
	int ret = BLE_Command2(&cmd, &resp, 100);
	
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
		
	return ERR_NONE;
}
/*
1.5.31.1. BLEPresetConfig
Command: [2500][Byte Array(17): BLEConfig]
Response: [00][Bool: Result]
Example
Command: 2500881300000A01A0000702020000D2040000
(BLEConfig: 881300000A01A0000702020000D2040000)
Response: 0001
(Result: true)
*/

#if 0
static int api_BLE_PresetConfig(buffer_t* command, buffer_t* response){
	typedef struct __PACKED  {
		uint8_t BLEConfig[17];
	}BLE_Config_param_t;
	BLE_Config_param_t* param = (BLE_Config_param_t*)command->data;

	return ERR_NONE;
}
#endif 

/*
1.5.31.2. BLEPresetUserData
Command: [2501][Byte: ScanResp][Byte Array(Var): UserData]
Response: [00][Bool: Result]
Example
Command: 2501001E0201061AFF4C000215E2C56DB5DFFB48D2B060D0F5A71096-
E000000000C3
(ScanResp: 00, UserData:
0201061AFF4C000215E2C56DB5DFFB48D2B060D0F5A71096E000000000C3)
Response: 0001
(Result: true)
*/
static int api_BLE_PresetUserData(buffer_t* command, buffer_t* response){
	typedef struct __PACKED  {
		uint8_t ScanResp;
		uint8_t UserDataLen;
		uint8_t UserData[];
	}BLE_PresetUserData_param_t;
	
	BLE_PresetUserData_param_t* param = (BLE_PresetUserData_param_t*)command->data;
	LTRACE_D("ScanResp=%d", param->ScanResp);
	LLOG_BUFS("UserData", param->UserData, param->UserDataLen);
	NET_BUF_SIMPLE_DEFINE(cmd_buf, 255);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 16);
	BLEPack_T cmd = {.code = BLE_Cmd_Adv, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};
	
	net_buf_simple_add_u8(cmd.data, param->ScanResp);
	net_buf_simple_add_u8(cmd.data, param->UserDataLen);
	net_buf_simple_add_mem(cmd.data, param->UserData, param->UserDataLen);
	
	int ret = BLE_Command2(&cmd, &resp, 200);
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
	
	return ERR_NONE;
}

/*
1.5.31.5. BLEGetAddress
Command: [2504]
Response: [00][Bool: Result][Byte Array(6): DeviceAddress][Byte Array(6): RemoteAddress][Byte
Array(1): RemoteType]
Example
Command: 2504
Response: 000149D702570B009872F9F36D4601
(Result: true, DeviceAddress: 49D702570B00, RemoteAddress: 9872F9F36D46, Re-
moteType: 01)
*/
static int api_BLE_GetAddress(buffer_t* command, buffer_t* response){

	NET_BUF_SIMPLE_DEFINE(cmd_buf, 1);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 64);
	BLEPack_T cmd = {.code = BLE_Cmd_GetMac, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};
	
	
	int ret = BLE_Command2(&cmd, &resp, 200);
	
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
	net_buf_simple_add_mem(response, resp.data->data, resp.data->len);
	
	return ERR_NONE;
}

/*
1.5.31.6. BLEGetVersion
Command: [2505]
Response: [00][Bool: Result][Byte Array(16): HWVersion][Byte Array(12): BootString]
Example
Command: 2505
Response: 000156312E30342C32382E30362E3230313702000400000018090000-0101
(Result: true, HWVersion: 56312E30342C32382E30362E32303137, BootString:
020004000000180900000101)

*/
static int api_BLE_GetVersion(buffer_t* command, buffer_t* response){
	NET_BUF_SIMPLE_DEFINE(cmd_buf, 1);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 64);
	BLEPack_T cmd = {.code = BLE_Cmd_GetVer, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};
	
	int ret = BLE_Command2(&cmd, &resp, 200);
	
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
	
	net_buf_simple_add_mem(response, resp.data->data, resp.data->len);
	
	return ERR_NONE;
}

int BLE_GetVersion(void){

	NET_BUF_SIMPLE_DEFINE(cmd_buf, 1);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 64);
	BLEPack_T cmd = {.code = BLE_Cmd_GetVer, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};
	
	int ret = BLE_Command2(&cmd, &resp, 200);
	
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
		
	return ERR_NONE;
}


/*
1.5.31.8. BLEGetGattServerAttributeValue
Command: [2507][UInt16: AttrHandle][Byte: MaxLen]
Response: [00][Bool: Result][Byte Array(Var): Data]
Example
Command: 2507120014
(AttrHandle: 1200, MaxLen: 14)
Response: 0001104254312E3035454C2020202020202020
(Result: true, Data: 4254312E3035454C2020202020202020)

*/
static int api_BLE_GetGattServerAttributeValue(buffer_t* command, buffer_t* response){
	typedef struct __PACKED  {
		uint16_t AttrHandle;
		uint8_t MaxLen;
	}BLE_GetGattServerAttributeValue_param_t;
	
	BLE_GetGattServerAttributeValue_param_t* param =
		(BLE_GetGattServerAttributeValue_param_t*)command->data;
	LTRACE_D("AttrHandle=0x%04X", param->AttrHandle);
	LTRACE_D("MaxLen=0x%02X", param->MaxLen);

	NET_BUF_SIMPLE_DEFINE(cmd_buf, 32);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 256);
	BLEPack_T cmd = {.code = BLE_Cmd_GetValue, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};

	net_buf_simple_add_le16(cmd.data, param->AttrHandle);
	net_buf_simple_add_le16(cmd.data, param->MaxLen);

	int ret = BLE_Command2(&cmd, &resp, 200);
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
	
	net_buf_simple_add_u8(response, resp.data->len);	
	net_buf_simple_add_mem(response, resp.data->data, resp.data->len);
	
	return ERR_NONE;
}

int BLE_GetGattServerAttributeValue(uint16_t AttrHandle, uint8_t* data, uint8_t* len){

	NET_BUF_SIMPLE_DEFINE(cmd_buf, 32);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 256);
	BLEPack_T cmd = {.code = BLE_Cmd_GetValue, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};

	net_buf_simple_add_le16(cmd.data, AttrHandle);
	net_buf_simple_add_le16(cmd.data, 16);

	int ret = BLE_Command2(&cmd, &resp, 200);
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
	if(resp.data->len == 0){
		return -1;
	}

	if(resp.data->len>16){			// 超过 16字节丢弃后面数据。
		resp.data->len = 16;
	}
	
	memcpy(data, resp.data->data, resp.data->len);
	*len = resp.data->len;
	
	return ERR_NONE;
}

/*
1.5.31.9. BLESetGattServerAttributeValue
Command: [2508][UInt16: AttrHandle][UInt16: Offset][Byte Array(Var): Data]
Response: [00][Bool: Result]
Example
Command: 250815000000050000000000
(AttrHandle: 1500, Offset: 0000, Data: 0000000000)
Response: 0001
(Result: true)
*/
static int api_BLE_SetGattServerAttributeValue(buffer_t* command, buffer_t* response){
	typedef struct __PACKED  {
		uint16_t AttrHandle;
		uint16_t Offset;
		uint8_t DataLen;
		uint8_t Data[];
	}BLE_SetGattServerAttributeValue_param_t;
	
	BLE_SetGattServerAttributeValue_param_t* param = 
		(BLE_SetGattServerAttributeValue_param_t*)command->data;
	LTRACE_D("AttrHandle=0x%04X", param->AttrHandle);
	LTRACE_D("Offset=0x%04X", param->Offset);
	LLOG_BUFS("Data", param->Data, param->DataLen);

	NET_BUF_SIMPLE_DEFINE(cmd_buf, 255);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 16);
	BLEPack_T cmd = {.code = BLE_Cmd_SetValue, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};
	
	net_buf_simple_add_le16(cmd.data, param->AttrHandle);
	net_buf_simple_add_le16(cmd.data, param->Offset);
	net_buf_simple_add_mem(cmd.data, param->Data, param->DataLen);

	int ret = BLE_Command2(&cmd, &resp, 200);
	
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
	
	return ERR_NONE;
}


/*
1.5.31.11. BLERequestEndpointClose
Command: [250A]
Response: [00][Bool: Result]
Example
Command: 250A
Response: 0001
(Result: true)
*/

static int api_BLE_RequestEndpointClose(buffer_t* command, buffer_t* response){
	
	NET_BUF_SIMPLE_DEFINE(cmd_buf, 1);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 16);
	
	BLEPack_T cmd = {.code = BLE_Cmd_Discon, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};
	
	int ret = BLE_Command2(&cmd, &resp, 200);
	
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
	
	return ERR_NONE;
}

int BLE_RequestEndpointClose(void){
	
	NET_BUF_SIMPLE_DEFINE(cmd_buf, 1);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 16);
	
	BLEPack_T cmd = {.code = BLE_Cmd_Discon, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};
	
	int ret = BLE_Command2(&cmd, &resp, 100);
	
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
	
	return ERR_NONE;
}


/*
1.5.31.11. BLERequestRssi
Command: [2509]
Response: [00][Bool: Result]
Example
Command: 2509
Response: 0001
(Result: true)
*/

static int api_BLE_RequestRssi(buffer_t* command, buffer_t* response){

	NET_BUF_SIMPLE_DEFINE(cmd_buf, 1);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 16);
	
	BLEPack_T cmd = {.code = BLE_Cmd_RequestRssi, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};
	
	int ret = BLE_Command2(&cmd, &resp, 200);
	
	LOG_D("RequestRssi!!! ret=%i, resp.code=%d", ret, resp.code);
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
	
	
	return ERR_NONE;
}

/*
1.5.31.7. BLEGetEnvironment
Command: [2506]
Response:[00][Bool: Result][Byte Array(1): DeviceRole][Byte Array(1): SecurityMode][Byte Array(1):
Rssi]
Example
Command: 2506
Response: 0001000000
(Result: true, DeviceRole: 00, SecurityMode: 00, Rssi: 00)
*/

static int api_BLE_GetEnvironment(buffer_t* command, buffer_t* response){

	NET_BUF_SIMPLE_DEFINE(cmd_buf, 1);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 16);
	BLEPack_T cmd = {.code = BLE_Cmd_GetRssi, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};
	
	int ret = BLE_Command2(&cmd, &resp, 200);

	
	LOG_D("getRssi!!! ret=%i, resp.code=%d", ret, resp.code);
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误

	net_buf_simple_add_u8(response, 0); 	// 固定补充 0, 不清楚具体含义
	net_buf_simple_add_u8(response, 0); 	// 固定补充 0, 不清楚具体含义
	net_buf_simple_add_u8(response, resp.data->data[0]);  //只上报数据域0 信号值，协议中信道丢弃
	
	return ERR_NONE;
}


/*
1.5.31.13. BLEFindGattServerAttribute
Command: [250C][Byte Array(Var): UUID]
Response: [00][Bool: Result][UInt16: AttrHandle]
Example
Command: 250C02262A
(UUID: 262A)
Response: 00011200
(Result: true, AttrHandle: 18)
*/
static int api_BLE_FindGattServerAttribute(buffer_t* command, buffer_t* response){
	typedef struct __PACKED  {
		uint8_t UUIDLen;
		uint8_t UUID[];
	}BLE_FindGattServerAttribute_param_t;
	
	BLE_FindGattServerAttribute_param_t* param = 
		(BLE_FindGattServerAttribute_param_t*)command->data;
	LLOG_BUFS("UUID", param->UUID, param->UUIDLen);

	NET_BUF_SIMPLE_DEFINE(cmd_buf, 64);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 64);
	
	BLEPack_T cmd = {.code = BLE_Cmd_GetGattHandle, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};
	net_buf_simple_add_u8(cmd.data, param->UUIDLen);
	net_buf_simple_add_mem(cmd.data, param->UUID, param->UUIDLen);

	int ret = BLE_Command2(&cmd, &resp, 200);
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
	
	net_buf_simple_add_mem(response, resp.data->data, resp.data->len);
	
	return ERR_NONE;
}

int BLE_FindGattServerAttribute(uint16_t * attrhandle){

	uint8_t uuid[16] = 
		{0xD6, 0x58, 0xD6, 0x21, 0xBC, 0x55, 0x81, 0x9F, 0x42, 0x44, 0x71, 0x6D, 0xC4, 0x6E, 0xC2, 0xFE};
	// 丘工提供的UUID
	NET_BUF_SIMPLE_DEFINE(cmd_buf, 64);
	NET_BUF_SIMPLE_DEFINE(rcv_buf, 64);
	
	BLEPack_T cmd = {.code = BLE_Cmd_GetGattHandle, .data = &cmd_buf};
	BLEPack_T resp = {.code = 1, .data = &rcv_buf};
	net_buf_simple_add_u8(cmd.data, 16);
	net_buf_simple_add_mem(cmd.data, uuid, 16);

	int ret = BLE_Command2(&cmd, &resp, 200);
	API_CHECK(ret);  	  //处理通讯错误
	API_CHECK(resp.code); // 处理蓝牙协议错误
	
	memcpy(attrhandle, resp.data->data, resp.data->len);
	
	return ERR_NONE;
}
/*--------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------*/
//自定义api
/*
蓝牙复位
*/
static int api_BLE_Reset(buffer_t* command, buffer_t* response){
	LTRACE_I("Reset");
	int ret = BLE_Reset();
	API_CHECK(ret);  	  //处理通讯错误
	return ERR_NONE;
}
/*--------------------------------------------------------------------------------------------------*/
/*
设置蓝牙名称
*/
static int api_BLE_SetName(buffer_t* command, buffer_t* response){
	typedef struct __PACKED  {
		uint8_t Len;
		uint8_t Name[];
	}BLE_SetName_param_t;
	BLE_SetName_param_t *param = (BLE_SetName_param_t *)command->data;
	LTRACE_BUFS(param->Name, param->Len);
	int ret = BLE_SetName(param->Name, param->Len);
	API_CHECK(ret);  	  //处理通讯错误
	uint8_t name[64];
	uint8_t nlen = 0;
	memset(name, 0, sizeof(name));
	for(int i = 0; i < 3; i++){
		ret = BLE_GetName(name, &nlen);
		if(!ret){
			break;
		}
	}
	API_CHECK(ret);  	  //处理通讯错误
	return ERR_NONE;
}
/*--------------------------------------------------------------------------------------------------*/
/*
获取蓝牙名称
*/
static int api_BLE_GetName(buffer_t* command, buffer_t* response){
	uint8_t rlen = 0;
	LTRACE_I("getName");
	int ret = BLE_GetName(response->data, &rlen);
	API_CHECK(ret);  	  //处理通讯错误
	response->len = rlen;
	net_buf_simple_push_u8(response, rlen);
	return ERR_NONE;
}
/*--------------------------------------------------------------------------------------------------*/
/*
进入boot模式
*/
static int api_BLE_Boot(buffer_t* command, buffer_t* response){
	LTRACE_I("DFU Boot");
	int ret = BLE_DFUBoot();
	API_CHECK(ret);  	  //处理通讯错误
	return ERR_NONE;
}
/*--------------------------------------------------------------------------------------------------*/
/*
升级
*/
static int api_BLE_Upgrade(buffer_t* command, buffer_t* response){
	typedef struct __PACKED  {
		uint32_t address;
		uint8_t Len;
		uint8_t Data[];
	}BLE_Upgrade_param_t;
	BLE_Upgrade_param_t* param = (BLE_Upgrade_param_t*)command->data;
	LTRACE_I("DFU Upgrade");
	uint8_t* buf = command->data;
	uint16_t length = param->Len + sizeof(param->address);
	memmove(&param->Len, param->Data, param->Len);
	int ret = BLE_DFUpgrade(buf, length);
	API_CHECK(ret);  	  //处理通讯错误
	return ERR_NONE;
}

/*--------------------------------------------------------------------------------------------------*/
/*
升级结束
*/
static int api_BLE_Finish(buffer_t* command, buffer_t* response){
	if(command->len != 5){
		TRACE_E("param len = %d", command->len);
		return -1;
	}
	LTRACE_I("DFU Finish");
	int ret = BLE_DFUFinish(command->data, command->len);
	API_CHECK(ret);  	  //处理通讯错误
	return ERR_NONE;
}
/*--------------------------------------------------------------------------------------------------*/
/*
获取蓝牙状态
*/
static int api_BLE_GetState(buffer_t* command, buffer_t* response){
	uint8_t state = 0;
	LTRACE_I("get State");
	int ret = BLE_GetState(&state);
	API_CHECK(ret);  	  //处理通讯错误
	net_buf_simple_add_u8(response, state);
	return ERR_NONE;
}

/*--------------------------------------------------------------------------------------------------*/
static const api_func_t func_ble_list[] = {
	{0x80, api_BLE_SetName},
	{0x81, api_BLE_GetName},
	{0x82, api_BLE_Reset},
	{0x83, api_BLE_Boot},
	{0x84, api_BLE_Upgrade},
	{0x85, api_BLE_Finish},
	{0x86, api_BLE_GetState},
};

/*--------------------------------------------------------------------------------------------------*/
static const api_entry_t api_ble_list[] = {
	[0x01] = api_BLE_PresetUserData,
	[0x02] = api_BLE_Init,
	[0x04] = api_BLE_GetAddress,
	[0x05] = api_BLE_GetVersion,
	[0x06] = api_BLE_GetEnvironment,
	[0x07] = api_BLE_GetGattServerAttributeValue,
	[0x08] = api_BLE_SetGattServerAttributeValue,
	[0x09] = api_BLE_RequestRssi,
	[0x0A] = api_BLE_RequestEndpointClose,
	[0x0C] = api_BLE_FindGattServerAttribute,
};


static int ble_call_api(buffer_t* command, buffer_t* response){
	const uint8_t num = sizeof(api_ble_list)/sizeof(api_ble_list[0]);
	uint8_t fun = net_buf_simple_pull_u8(command);
	
	if(fun < num){
		if(api_ble_list[fun]){
			return api_ble_list[fun](command, response);
		}
		return ERR_UNKNOWN_FUNCTION;
	}
	
	const uint8_t func_num = sizeof(func_ble_list)/sizeof(func_ble_list[0]);
	for(int i = 0; i < func_num; i++){
		if(func_ble_list[i].func == fun){
			return func_ble_list[i].entry(command, response);
		}
	}
	
	return ERR_INVALID_FUNCTION;
}

static int ble_api(buffer_t* command, buffer_t* response){
//		const uint8_t num = sizeof(api_ble_list)/sizeof(api_ble_list[0]);
//		int ret = protocol_call_api(api_ble_list, num, command, response);
	int ret = ble_call_api(command, response);
	if(ret > 0){
		return ret;
	}
	protocol_response_result(response, ret);
	return ERR_NONE;
}

SIMPLE_API_DEFINE(API_CLASS_BLE, ble_api);


